import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import TaskModel 1.0
import RewardsModel 1.0

Window {
    width: 800
    height: 700
    visible: true
    title: "积分任务奖励系统"

    // 定义TaskModel实例
    TaskModel {
        id: taskModel
    }

    RewardsModel {
        id: rewardsModel
    }

    ScrollView {
        anchors.fill: parent
        anchors.margins: 20

        ColumnLayout {
            width: parent.width - 40
            spacing: 20

            // 用户信息面板
            UserInfoPanel {
                Layout.fillWidth: true
            }

            // 任务管理面板
            TaskManagementPanel {
                Layout.fillWidth: true
                taskModel: taskModel
            }

            // 奖励管理面板
            RewardManagementPanel {
                Layout.fillWidth: true
                rewardsModel: rewardsModel
            }
        }
    }

    // 编辑任务积分对话框
    Dialog {
        id: editTaskDialog
        title: "编辑任务积分"
        modal: true
        standardButtons: Dialog.Ok | Dialog.Cancel

        property int taskIndex: -1
        property string taskName: ""
        property int executeCount: 0
        property var dayPoints: []

        ColumnLayout {
            width: parent.width
            spacing: 10

            Label {
                text: "任务: " + editTaskDialog.taskName
                font.bold: true
            }

            Label {
                text: "执行次数: " + editTaskDialog.executeCount
            }

            Label {
                text: "编辑每日积分:"
                font.bold: true
            }

            GridLayout {
                columns: 4
                width: parent.width

                Repeater {
                    model: 7

                    ColumnLayout {
                        spacing: 2

                        Label {
                            text: "第" + (index + 1) + "天"
                            font.pixelSize: 10
                        }

                        SpinBox {
                            id: editDayPointField
                            from: 0
                            to: 100
                            value: editTaskDialog.dayPoints && editTaskDialog.dayPoints.length > index ?
                                   editTaskDialog.dayPoints[index] : 0
                            onValueChanged: {
                                if (editTaskDialog.dayPoints && editTaskDialog.dayPoints.length > index) {
                                    var points = editTaskDialog.dayPoints.slice();
                                    points[index] = value;
                                    editTaskDialog.dayPoints = points;
                                }
                            }
                        }
                    }
                }
            }
        }

        onAccepted: {
            // 更新任务积分
            for (var i = 0; i < 7; i++) {
                taskModel.updateDayPoints(editTaskDialog.taskIndex, i, editTaskDialog.dayPoints[i]);
            }
        }
    }
}
